package homeactivity.PingpaiFragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.melnykov.fab.FloatingActionButton;
import com.yztc.thinkpad.mandemo.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import homeactivity.bean.PingpaiContentBean;
import homeactivity.contans.PingpaiUrl;
import homeactivity.pingpaiadapter.GrideAdapter;
import utils.DividerGridItemDecoration;
import utils.Okhttputils;
/**
 * 品牌精选的第一个activity的Fragment
 * Created by Administrator on 2016/10/11.
 */
public class FirstFragment extends android.support.v4.app.Fragment{
    private  String url;
    private  String mBrandID;
    private  View mView;
    private int  pageIndex=1;
    private RecyclerView mRecyclerView;
    private FloatingActionButton mFab;
    private GrideAdapter adapter;
    private GridLayoutManager manager;
    private List<PingpaiContentBean.DataBean> list;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(final Message msg) {
            super.handleMessage(msg);
            if (pageIndex==1){
                list= (List<PingpaiContentBean.DataBean>) msg.obj;
                adapter=new GrideAdapter(getActivity(),list);
                mRecyclerView.setAdapter(adapter);
            }else{
                list.addAll((List<PingpaiContentBean.DataBean>) msg.obj);
                adapter.notifyDataSetChanged();
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mView=inflater.inflate(R.layout.pingpai_firstfragment,container,false);
            Bundle bundle=getArguments();
        if (bundle!=null){
            mBrandID=bundle.getString("mBrandID");
//            Log.i("tag","--------->"+mBrandID);
        }
        url=PingpaiUrl.URLHEAD+mBrandID+PingpaiUrl.PATHINDEX+pageIndex+PingpaiUrl.URLFOOT;
                initData();//初始化控件
                initView();
        manager=new GridLayoutManager(getActivity() ,2);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        return mView;
    }
    private void initView() {
        new  Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String jsonString= Okhttputils.getInstance().run(url);
                    String data = JSONObject.parseObject(jsonString).getString("data");
                    List<PingpaiContentBean.DataBean> datas=new ArrayList<PingpaiContentBean.DataBean>();
                    datas= JSONArray.parseArray(data,PingpaiContentBean.DataBean.class);
                    Message message=new Message();
                    message.obj=datas;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void initData() {
        mRecyclerView= (RecyclerView) mView.findViewById(R.id.pingpai_recyle);
        mFab= (FloatingActionButton) mView.findViewById(R.id.pingpai_fab);
        mFab.attachToRecyclerView(mRecyclerView);
        //FloatingActionButton的点击事件  点击置顶
        mFab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
//               Toast.makeText(getActivity(), "稍后点击",Toast.LENGTH_LONG).show();
                manager.scrollToPositionWithOffset(0,0);
           }
       });
    }
}
